package com.app.controller;

import com.app.enums.StatusCodeEnum;
import com.app.mapper.jiuquan.SiteApplyForMapper;
import com.app.model.*;
import com.app.service.*;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.SiteApplyForVo;
import com.app.vo.TrainPeopleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

//培训学员报名信息
@RestController
@Controller
@RequestMapping("/jiuquan/site")
public class SiteApplyForController {
    @Autowired
    SiteApplyForMapper siteApplyForMapper;
    @Autowired
    SiteApplyForService siteApplyForService;
    @Autowired
    UserService userService;
    @Autowired
    AdminUserService adminUserService;
    //查询列表
    @GetMapping("/getSiteApplyForList")
    public Result getTrainPeopleList(SiteApplyForModel siteApplyForModel) {
        SiteApplyForVo vo = siteApplyForService.getListByModel(siteApplyForModel);
        return Result.success(vo);
    }


    @PostMapping("/addSiteApplyForList")
    public Result<Object> addSiteApplyForList(
            @RequestBody SiteApplyForModel model,
            HttpServletRequest request
    ) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(StatusCodeEnum.STATUS405.getCode(), StatusCodeEnum.STATUS405.getMsg());
        }
        String openId = TokenUtils.nothingness(token);
        JiuquanUserModel user = userService.getUserByOpenid(openId);
        if (user == null) {
            return Result.error(StatusCodeEnum.STATUS406.getCode(), StatusCodeEnum.STATUS406.getMsg());
        }
//        用户id
        Integer userId = user.getId();

        model.setUserId(userId);
        model.setCreateTime(CommonHelp.getNowTimestamp());
        siteApplyForMapper.insert(model);
        return Result.success("操作成功");
    }

    /**
     * 我的场地申请
     * @param model
     * @return
     */
    @GetMapping("/getMySiteApplyForList")
    public Result getMySiteApplyForList(HttpServletRequest request,SiteApplyForModel model) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(StatusCodeEnum.STATUS405.getCode(), StatusCodeEnum.STATUS405.getMsg());
        }
        String openId = TokenUtils.nothingness(token);
        JiuquanUserModel user = userService.getUserByOpenid(openId);
        if (user == null) {
            return Result.error(StatusCodeEnum.STATUS406.getCode(), StatusCodeEnum.STATUS406.getMsg());
        }
//        用户id
        Integer userId = user.getId();
        model.setUserId(userId);
        SiteApplyForVo vo = siteApplyForService.getMyListByUserId(model);
        return Result.success(vo);
    }

    @PostMapping("/siteApplyForStare")
    public Result<Object> siteApplyForStare(
            @RequestBody SiteApplyForModel model,
            HttpServletRequest request
    ) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        SiteApplyForModel siteApplyForModel = siteApplyForMapper.selectById(model.getId());
        siteApplyForModel.setState(model.getState());
        siteApplyForMapper.updateById(siteApplyForModel);
        return Result.success("操作成功");
    }

    /**
     * 我的场地申请
     * @param id
     * @return
     */
    @GetMapping("/getSiteApplyForById")
    public Result getSiteApplyForById(Integer id) {
        SiteApplyForModel model = siteApplyForMapper.selectById(id);
        return Result.success(model);
    }

}
